﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Access.PowerTools.Utilities;
using Access.PowerTools.Settings;
using Access.PowerTools.AbstractionLayer;
using SMSConsulting.SMSLIB.Utilities;

namespace Access.PowerTools.Add_In.Engine
{
    public class CommandsProcessor
    {
        public static void BackUp()
        {
            executeBackUp();
            //Dialogs.ShowInfoMessage("Back-Up finished: " + appNameAndVersion); 
        }

        public static void Restore()
        {
            executeRestore();
            //Dialogs.ShowInfoMessage("Restore: " + appNameAndVersion); 
        }

        public static void About()
        {
            Access.PowerTools.Settings.Globals.ShowAboutBox();  
        }

        private static string appNameAndVersion
        {
            get
            {
                AddInCommunicationBridge bridge =
                Access.PowerTools.Settings.Globals.HostApplication as
                 AddInCommunicationBridge;

                IHostComunicationBridge iHost =
                    bridge as IHostComunicationBridge;

                return string.Format("{0} v.{1}",
                iHost.ApplicationName, iHost.ApplicationVersion);
            }
        }

        private static bool hasCurrentDbOpened(IHostComunicationBridge iHost)
        {
            try
            {
                List<AccessDatabaseObjectInfo> list = iHost.GetObjectsList();
                return (list != null && list.Count >=0 );  
            }
            catch
            {
                return false;
            }
        }

        private static void executeBackUp()
        {
            string message = "";

            AddInCommunicationBridge bridge = null;
            IHostComunicationBridge iHost = null;
            AccessApplication application = null;
            Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent backup = null;

            try
            {
                bridge =
                  Access.PowerTools.Settings.Globals.HostApplication as
                    AddInCommunicationBridge;

                iHost =
                    bridge as IHostComunicationBridge;

                bool hasCurrentDb =
                    hasCurrentDbOpened(iHost);

                if (!hasCurrentDb)
                {
                    throw new ApplicationException("There is no current DB opened."); 
                }

                List<AccessDatabaseObjectInfo> list = iHost.GetObjectsList();

                application = new AccessApplication();

                backup = 
                    new Access.PowerTools.Database.BackUp.Plug_In.BackUpAgent(application);

                string dbFolder =
                    System.IO.Path.GetDirectoryName(iHost.CurrentDatabaseFullPath);
                string backUpFolder =
                    System.IO.Path.Combine(dbFolder, "BackUp");
                if (!System.IO.Directory.Exists(backUpFolder))
                    System.IO.Directory.CreateDirectory(backUpFolder);     

                //Access.PowerTools.Utilities.FilesAndFolders.ClearFolder(backUpFolder);

                //backup.ReportProgressStatus += new EventHandler<RunProgressEventArgs>(ReportProgressStatus);

                //backup.BackUpDatabase(backUpFolder);

                Access.PowerTools.GUI.Dialogs.DisplayBackupRestoreDialog(true, backup, backUpFolder);     

                //message = string.Format("Backup to folder '{0}' finished OK.", backUpFolder); 
                //Dialogs.ShowInfoMessage(message);  
            }
            catch (ApplicationException ex)
            {
                Dialogs.ShowWarningMessage(ex.Message);
            }
            catch (Exception ex)
            {
                SimpleLogWriter.WriteLogLine(ex.Message);
                Dialogs.ShowErrorMessage(ex.Message);
            }
            finally
            {
                backup = null;
                application = null;
                iHost = null;
                bridge = null;
                GC.Collect(); 
            }
        }

        private static void executeRestore()
        {
            string message = "";
            AddInCommunicationBridge bridge = null;
            IHostComunicationBridge iHost = null;
            AccessApplication application = null;
            Access.PowerTools.Database.Restore.Plug_In.RestoreAgent restore = null;

            try
            {
                bridge =
                  Access.PowerTools.Settings.Globals.HostApplication as
                    AddInCommunicationBridge;

                iHost =
                    bridge as IHostComunicationBridge;

                bool hasCurrentDb =
                    hasCurrentDbOpened(iHost);

                if (!hasCurrentDb)
                {
                    throw new ApplicationException("There is no current DB opened.");
                }
                List<AccessDatabaseObjectInfo> list = iHost.GetObjectsList();

                application = new AccessApplication();

                restore = new Access.PowerTools.Database.Restore.Plug_In.RestoreAgent(application);


                string currentDbFullPath = iHost.CurrentDatabaseFullPath;
                string dbFolder =
                    System.IO.Path.GetDirectoryName(iHost.CurrentDatabaseFullPath);
                string backUpFolder =
                    System.IO.Path.Combine(dbFolder, "BackUp");

                //if (!System.IO.Directory.Exists(dbFolder))
                //{
                //    message = string.Format(
                //        "Backup to folder '{0}' doesn't exist.", backUpFolder);
                //    throw new ApplicationException(message);
                //}

                //backup.ReportProgressStatus += new EventHandler<RunProgressEventArgs>(ReportProgressStatus);

                //restore.RestoreDatabase(backUpFolder);

                //message = string.Format("Restore from folder '{0}' finished OK.", backUpFolder);
                //Dialogs.ShowInfoMessage(message);

                Access.PowerTools.GUI.Dialogs.DisplayBackupRestoreDialog(false, restore, backUpFolder);     

            }
            catch (ApplicationException ex)
            {
                Dialogs.ShowWarningMessage(ex.Message);
            }
            catch (Exception ex)
            {
                SimpleLogWriter.WriteLogLine(ex.Message);
                Dialogs.ShowErrorMessage(ex.Message);
            }
            finally
            {
                restore = null;
                application = null;
                iHost = null;
                bridge = null;
                GC.Collect();
            }
        }

    }
}
